package com.polaris.live.adapter

import android.text.BidiFormatter
import android.text.TextDirectionHeuristics
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.LifecycleCoroutineScope
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.common.ext.convertArabicDigitsToNormal
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.manager.CoroutineScopeManager
import com.polaris.live.resp.back_resp.UserMedalVo
import com.polaris.live.translation.R.string
import com.polaris.live.utils.CommonUtils
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.playAsync
import com.polaris.live.utils.route.RouterUtils
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder
import kotlinx.coroutines.launch
import org.libpag.PAGImageView
import java.util.Locale

/**
 * HonorDialogAdapter
 *
 * @author Created by 半仙 on 2024/1/29/029 15:04
 */
class HonorDialogAdapter(
    private val mNeedRouter: Boolean,
    private val lifecycleScope: LifecycleCoroutineScope
) : BaseBannerAdapter<UserMedalVo>() {

    override fun bindData(
        holder: BaseViewHolder<UserMedalVo>?,
        data: UserMedalVo?,
        position: Int,
        pageSize: Int,
    ) {
        holder?.apply {
            val image = findViewById<ImageView>(R.id.honor_item_icon)
            val pag = findViewById<PAGImageView>(R.id.honor_item_pag)
            val name = findViewById<TextView>(R.id.honor_item_name)
            val detail = findViewById<TextView>(R.id.honor_item_detail)
            val timeText = findViewById<TextView>(R.id.honor_item_get_time)
            val myLevelText = findViewById<TextView>(R.id.honor_item_go_to)
            val shapeView = findViewById<View>(R.id.shape_view)
            if (data?.pagUrl?.isNotEmpty() == true) {
                pag.playAsync(lifecycleScope, data.pagUrl) { pagFile ->
                    setRepeatCount(-1)
                    try {
                        lifecycleScope.launch(CoroutineScopeManager.ioDispatcher) {
                            CommonUtils.pagReplace(context, pagFile, data.layers)
                        }
                    } catch (_: Exception) {
                    }
                }
                pag.visibility = View.VISIBLE
                image.visibility = View.GONE
            } else {
                pag.visibility = View.GONE
                image.visibility = View.VISIBLE
                Glide.with(image).load(ImageUtils.imgPath(data?.photoUrl)).into(image)
            }
            name.text = data?.name
            detail.text = data?.description
            if (data?.createTime == null) {
                timeText.visibility = View.GONE
            } else {
                timeText.visibility = View.VISIBLE
                val millis2String =
                    TimeUtils.millis2String(data.createTime, "yyyy-MM-dd")
                        .convertArabicDigitsToNormal()
                val time = BidiFormatter.getInstance(Locale.getDefault()).unicodeWrap(
                    millis2String, TextDirectionHeuristics.LTR
                )
                timeText.text = StringUtils.getString(string.about_time, time)
            }
            myLevelText.setOnSingleClickListener {
                data?.routerUrl?.let {
                    RouterUtils.routerGoto(it)
                }
            }
            if (mNeedRouter) {
                CommonUtils.canKeepOn({
                    if (data?.routerTitle != null) {
                        myLevelText.visibility = View.VISIBLE
                        myLevelText.text = data.routerTitle
                    } else {
                        myLevelText.visibility = View.GONE
                    }
                }, {
                    myLevelText.visibility = View.GONE
                })
            } else {
                myLevelText.visibility = View.GONE
            }
            shapeView.setOnSingleClickListener {
                onClickBackListener?.invoke()
            }
        }
    }

    override fun getLayoutId(viewType: Int) = R.layout.adapter_honor_item_dialog

    private var onClickBackListener: (() -> Unit)? = null
    fun setOnClickBackListener(l: () -> Unit) {
        onClickBackListener = l
    }
}